class Solution
{
public:
    int minimumLength(string s)
    {
        int front = 0;
        int back = s.size() - 1;
        while (front < back)
        {
            if (s[front] != s[back])
            {
                break;
            }
            int nextFront = front + 1;
            int nextBack = back - 1;
            while (nextFront <= nextBack)
            {
                if (s[nextFront] == s[front])
                {
                    ++nextFront;
                }
                else if (s[nextBack] == s[back])
                {
                    --nextBack;
                }
                else
                {
                    break;
                }
            }
            front = nextFront;
            back = nextBack;
        }
        return back - front + 1;
    }
};